// Chromanova!!!

/*
 * Benjamin Hitov
 * Ricky Mondello
 * Brooke Evans
 * Daniel Millstein
 *
 * Player.java - Implements DynamicBall. Controls player status.
 */


package chromafinal;

import java.awt.*;


public class Player extends DynamicBall
{

    //How long after death the player is invincible, in ms
    static final long INVINCIBLE_TIME = 1200;

    //Wall bounciness
    static final double DAMPING_STRENGTH = 0.1;

    //Player size
    static final int SIZE_OF_MY_BALLS = 16;

    Color pred;
    Color pgreen;
    
    long dTime;

    boolean alive;

    int deaths;


    public Player(double x, double y, int num0, Color pred0, Color pgreen0)
    {
        super(x,y,75,SIZE_OF_MY_BALLS,num0, pred0);
        cnum = 1;
        dTime = System.currentTimeMillis() - INVINCIBLE_TIME;
        pred = pred0;
        pgreen = pgreen0;
        deaths = 0;
    }

    //Moves the player in a period millisecond step based on already calculated
    //forces
    public void move(int period)
    {

        super.move(period);
        if (xl < 10){
            xl = 10;
            xv *= - DAMPING_STRENGTH;}
        else if (xl > 690){
            xl = 690;
            xv *= - DAMPING_STRENGTH;}

        if (yl < 10){
            yl = 10;
            yv *= - DAMPING_STRENGTH;}
        else if (yl > 460){
            yl = 460;
            yv *= - DAMPING_STRENGTH;}
    }

    //Change color from red to green or green to red
    public void cToggle()
    {
        cnum = cnum * -1;
        if (cnum == 1)
            color = pred;
        if (cnum == -1)
            color = pgreen;
    }

    //Checks time to see if player is invincible
    public boolean invincible()
    {
        if (System.currentTimeMillis() - dTime> INVINCIBLE_TIME)
            return false;
        return true;
    }

    //Tries to kill player, fails if player is invincible
    public boolean die()
    {
        if (!invincible()){
            deaths++;
            dTime = System.currentTimeMillis();
            new SoundPlayback("chromafinal/sounds/gameover.aif").start();
            
            xl = 50; yl = 150 * num; xv = 0; yv = 0; xa = 0; ya = 0;
            return true;
        }
        return false;
    }


    //Draws player to screen. A white border is added if the player is
    //invincible
    public void draw(Graphics g)
    {
        if (invincible())
            {
                g.setColor(Color.white);
                g.fillOval((int) xl-size/2-1, (int) yl-size/2-1,
                        size+2, size+2);
            }
        super.draw(g);
    }
    
    public int[] getRelCoords()
    {
        return new int[] {(int) (xl + Game.screenloc), (int) yl};
    }

    public int getRelX()
    {
        return (int)(xl + Game.screenloc);
    }
}